package com.samsung.svmc.moneymanager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

import com.samsung.svmc.moneymanager.data.DbTaiKhoan;
import com.samsung.svmc.moneymanager.data.DbTienTe;
import com.samsung.svmc.moneymanager.object.TaiKhoan;
import com.samsung.svmc.moneymanager.object.TheLoaiTaiKhoan;
import com.samsung.svmc.moneymanager.object.TienTe;
import com.samsung.svmc.moneymanager.utility.SingletonSystem;
import com.samsung.svmc.moneymanager.utility.TheLoaiTaiKhoanUtility;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Home extends Activity {

	//Cac bien he thong
	protected String tagSQL = "SQLConnection";
	protected String tagException = "Exception";
	protected String tagDebug = "Debug";
	protected String version = "version=1.0.0";
	protected String appName = "appname=Money Manager";
	protected String runCount = "count=0";
	protected HashMap<String, String> config = new HashMap<String, String>();
	protected boolean isHome = true;
	//Cac Activity
	protected int ACTIVITY_CREATE_ACCOUNT = 100;
	//Cac dieu khien
	Button btn_move_activity_add_acc;
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Toast.makeText(getApplicationContext(), "is home", 1).show();
        File dir = getFilesDir();
        File f = new File(dir.getAbsoluteFile()+"/","config.ini");
        if(!f.exists()){
        	try{
        		f.createNewFile();
        		//tao cac bien cau hinh he thong
        		String result = appName+"\r\n"+version+"\r\n"+runCount;
        		FileOutputStream out = new FileOutputStream(f);
        		out.write(result.getBytes());
        		out.flush();
        		out.close();
        	}catch(IOException ex){
        		Log.d(tagException, "loi tao file cau hinh: "+ex.getMessage());
        	}
        }
        /* doc file cau hinh lay cac thong tin */
        try{
        	FileInputStream in = new FileInputStream(f);
        	Scanner scan = new Scanner(in);
        	while(scan.hasNextLine()){
        		String st = scan.nextLine();
        		st = st.trim();
        		String[] str = st.split("=");
        		String key = str[0];
        		String value = str[1];
        		config.put(key, value);
        	}
        	scan.close();
        }catch(IOException ex){
        	Log.d(tagException, "loi doc file cau hinh: "+ex.getMessage());
        }
        SingletonSystem.getInstance().setConfig(config);
        int count = Integer.parseInt(config.get("count"));
        if(count == 0){
        	//layout khoi tao      	
        	setContentView(R.layout.ini_ui);
        	isHome = false;
        }
        else{
        	//layout chinh
        	setContentView(R.layout.activity_home);
        }
        SingletonSystem.getInstance();
        
        // khoi tao cac dieu khien
        if(isHome){
        	// Home Activiti
        }
        else{
        	//Ini Activity
        	
        	btn_move_activity_add_acc = (Button)findViewById(R.id.btnMoveCreateAcc);
        	btn_move_activity_add_acc.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent data = new Intent(getApplicationContext(),CreateAccount.class);
		        	data.putExtra("config", config);
		        	startActivityForResult(data, ACTIVITY_CREATE_ACCOUNT);
				}
			});
        }        
        
        
        /*
        TienTe t = new TienTe();
        t.set_DonViTinh(1000);
        t.set_Id(1);
        t.set_KiHieu("VNĐ");
        t.set_TenTienTe("Việt nam đồng");
        DbTienTe.getInstance(this).open().insert(t);
        t.set_DonViTinh(1);
        t.set_Id(2);
        t.set_KiHieu("$");
        t.set_TenTienTe("Dolar");
        DbTienTe.getInstance(this).insert(t);
        DbTienTe.getInstance(this).close();
        */
        /*TEST XML**/
        /*TheLoaiTaiKhoanUtility tltk = new TheLoaiTaiKhoanUtility(this);
        List<TheLoaiTaiKhoan> lv = tltk.XmlUtilyti();
        for(TheLoaiTaiKhoan tk:lv){
        	Log.d(tagDebug, "id tltk: "+tk.getId());
        	Log.d(tagDebug, "name tltk: "+tk.getTenTheLoai());
        }*/
        /***********************************************************************************/        
        //SingletonSystem.getInstance().setCountActivityBySub();
        //finish();        
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to
        if (requestCode == ACTIVITY_CREATE_ACCOUNT) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK) {
                Toast.makeText(getApplicationContext(), "save", 1).show();
                setContentView(R.layout.activity_home);
            } else if(resultCode == RESULT_CANCELED){
            	Toast.makeText(getApplicationContext(), "back", 1).show();
            	setContentView(R.layout.ini_ui);
            }
        }
    }
    
}
